package com.zhiyou100.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.ResultObject;
import com.zhiyou100.model.User;
import com.zhiyou100.service.ReUserControllerService;
import com.zhiyou100.util.FastDFS;

@Controller
public class ReUserController {
	@Autowired
	private ReUserControllerService ruc;
	
	@RequestMapping("/front/user/list")
	public String ReUserList(@RequestParam(defaultValue="1")int pageNo,
			@RequestParam(defaultValue="")String username,
			@RequestParam(defaultValue="")String userphone,
			@RequestParam(defaultValue="")String userstatus,Model model) {
		// 模糊查询
		Map<String,Object> map = new HashMap<>();
		map.put("username", username);
		map.put("userphone", userphone);
		map.put("userstatus", userstatus);
		// 分页
		PageHelper.startPage(pageNo, 3);
		//调用业务层
		List<User> user = ruc.findAll(map);
		PageInfo<User> pageInfo = new PageInfo<>(user);
		System.out.println("user: "+user);
		model.addAttribute("user", pageInfo.getList());
		model.addAttribute("map", map);
		model.addAttribute("page", pageInfo);
		// 控制跳转
		return "forward:/admin/view/user/UserList.jsp";
	}
	
	
	
	// 删除
	@RequestMapping("/front/user/delete")
	public String userDelete(@RequestParam(defaultValue="")String id,Model model) {
		System.out.println("id: "+id);
		ruc.userDelete(id);
		System.out.println("删除成功");
		return "forward:/front/user/list";
	}
	
	// 编辑
	@RequestMapping("/front/user/update")
	public String findUserById(@RequestParam(defaultValue="")String id,Model model) {
		System.out.println("id: "+id);
		User user = ruc.findUserById(id);
		System.out.println("user: "+user);
		model.addAttribute("user", user);
		return "forward:/admin/view/user/UserDetail.jsp";
	}
	@RequestMapping("/front/user/datail")
	public String updateUser(User user) {
		System.out.println("---------");
		System.out.println("user: "+user);
//		System.out.println("id: "+name);
		ruc.updateUser(user);
		return "forward:/front/user/list";
	}
	
    @RequestMapping(value="/front/user/check")
	public String userCheck(@RequestParam(defaultValue="1")int pageNo,
			@RequestParam(defaultValue="")String username,
			@RequestParam(defaultValue="")String userphone,
			@RequestParam(defaultValue="")String userstatus,Model model) {
		Map<String,Object> map = new HashMap<>();
		map.put("username", username);
		map.put("userphone", userphone);
		map.put("userstatus", userstatus);
		System.out.println("用户审核的遍历controller");
		// 分页
		PageHelper.startPage(pageNo, 3);
		//调用业务层
		List<User> user = ruc.findAll(map);
		PageInfo<User> pageInfo = new PageInfo<>(user);
		System.out.println("user: "+user);
		model.addAttribute("user", pageInfo.getList());
		model.addAttribute("map", map);
		model.addAttribute("page", pageInfo);
		// 控制跳转
		return "forward:/admin/view/user/UserCheck.jsp";
	}
	
	@RequestMapping(value="/front/user/check2")
	public String userCheck2(String id,Model model) {
		System.out.println("单个用户审核查找到的id: "+id);
		User user = ruc.findUserById(id);
		System.out.println("user: "+user);
		model.addAttribute("user", user);
		return "forward:/admin/view/user/UserCheck2.jsp";
	}
	
	@RequestMapping("/front/user/check3")
	public String userCheck3(int uStatus) {
		System.out.println("用户审核的uStatus: "+uStatus);
		ruc.userCheck(uStatus);
		
		return "forward:/front/user/list";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	// 图片上传
	@RequestMapping("/upload")
	@ResponseBody
	public ResultObject ajaxFastDFS(MultipartFile photo) throws IOException, MyException {
		System.out.println("photo: "+photo);
		String path = FastDFS.upload(photo);
		return new ResultObject("200","成功",path);
	}
	
}
